﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text.RegularExpressions;

namespace Jy123.Web.PersonalInfo
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AlterEmail : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            int userID;
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string msg = string.Empty;
            string strNewEmail = context.Request["SetEmail"];
            string emailRegex = @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
            if (Regex.IsMatch(strNewEmail, emailRegex))
            {
                if (CheckEmailAvailability(strNewEmail))
                {
                    Jy123.Model.User_Main mUser = new Jy123.Model.User_Main();
                    Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
                    mUser = bllUser.GetModel(userID);
                    mUser.Email = strNewEmail;
                    bllUser.Update(mUser);
                    msg = "修改成功！请牢记您的新邮箱,ok";
                }
                else
                {
                    msg = "Email已存在！,error";
                }
            }
            else
            {
                msg = "请输入正确的Email地址,error";
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(msg);
        }

        public bool CheckEmailAvailability(string email)
        {
            Jy123.BLL.User.UserSql bllUserSql = new Jy123.BLL.User.UserSql();
            bool emailIsAvailable = bllUserSql.CheckEmailAvailability(email);
            if (emailIsAvailable) { return true; }
            return false;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
